
#include "qcustomcalendarwidget.h"

#include <QLocale>
#include <QPainter>
#include <QTextCharFormat>
#include <QProxyStyle>
#include <QTableView>
#include <QLayout>
#include <QPushButton>
#include <QLabel>

class QCustomStyle : public QProxyStyle
{
public:
    QCustomStyle(QWidget *parent)
    {
        setParent(parent);


    };

private:
    void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
        QPainter *painter, const QWidget *widget) const
    {
        if (element == PE_FrameFocusRect)
        {
            return;
        }
        QProxyStyle::drawPrimitive(element, option, painter, widget);
    }
};

QCustomCalendarWidget::QCustomCalendarWidget(QWidget *parent,int startFilg)
    : QCalendarWidget(parent),startFilg_(startFilg)
{
    initControl();
    //this->setFixedSize(342, 343);

    setStyleSheet(

        "QCalendarWidget QAbstractItemView {"
        "  selection-background-color: transparent;" // 选中日期的背景颜色
        "  selection-color: white;" // 选中日期的字体颜色
        "}"
        );
}

QCustomCalendarWidget::~QCustomCalendarWidget()
{

}

void QCustomCalendarWidget::setRangeDate(const QDate &data)
{
    dateRange_ = data;
    this->updateCells();
}

void QCustomCalendarWidget::initControl()
{
    //layout()->setSizeConstraint(QLayout::SetFixedSize);

    setLocale(QLocale(QLocale::Chinese));
    setNavigationBarVisible(false);
    setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
    setStyle(new QCustomStyle(this));

    QTextCharFormat format;
    format.setForeground(QColor(160, 160, 160));
    format.setBackground(QColor(255, 255, 255));

    setHeaderTextFormat(format);
    setWeekdayTextFormat(Qt::Saturday, format);
    setWeekdayTextFormat(Qt::Sunday,   format);
    setWeekdayTextFormat(Qt::Monday,   format);
    setWeekdayTextFormat(Qt::Tuesday,  format);
    setWeekdayTextFormat(Qt::Wednesday,format);
    setWeekdayTextFormat(Qt::Thursday, format);
    setWeekdayTextFormat(Qt::Friday,   format);


    initTopWidget();
    //initBottomWidget();

    connect(this, &QCalendarWidget::currentPageChanged, [this](int year, int month){
        setDataLabelTimeText(year, month);
    });


}

void QCustomCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date)const
{


    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(Qt::NoPen);

    //选中单元格
    if (date == selectedDate())
    {
        painter->setBrush(QColor(230, 230, 230));

        // 计算圆形的中心和半径
        int radius = qMin(rect.width(), rect.height()) / 2; // 半径
        QPoint center = rect.center(); // 圆心

        //是否渲染灰色区域
        if(date == dateRange_)
        {
            if(startFilg_){
                painter->drawRect(rect.x() + rect.width() / 2, rect.y() + 3, rect.width() / 2 + 3, rect.height() - 6);
            }
            else{
                painter->drawRect(rect.x(), rect.y() + 3, rect.width() / 2, rect.height() - 6);
            }

            painter->drawEllipse(center, radius - 3, radius - 3);
        }

        //蓝圆
        painter->setBrush(QColor(0, 161, 255));
        painter->drawEllipse(center, radius - 8, radius - 8);

        //白字
        painter->setPen(QColor(255, 255, 255));
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));

    }


//    //今日日期
//    else if (date == QDate::currentDate())
//    {
//        painter->save();
//        painter->setRenderHint(QPainter::Antialiasing);
//        painter->setPen(Qt::NoPen);
//        painter->setBrush(QColor(0, 161, 255));
//        painter->drawRoundedRect(rect.x(), rect.y() + 3, rect.width(), rect.height() - 6, 3, 3);
//        painter->setBrush(QColor(255, 255, 255));
//        painter->drawRoundedRect(rect.x() + 1, rect.y() + 4, rect.width() - 2, rect.height() - 8, 2, 2);
//        painter->setPen(QColor(0, 161, 255));
//        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
//        painter->restore();
//    }

    //超出范围

    else if (dateRange_ != QDate() && date.month() == monthShown() &&
             (
                 //开始日历与结束日历刷新策略不同
                (date > dateRange_  &&  startFilg_) ||  (date < dateRange_  &&  !startFilg_)
             )


             )
    {
        painter->setBrush(QColor(230, 230, 230));
        painter->drawRect(rect.x(), rect.y() + 3, rect.width(), rect.height() - 6);
        painter->setPen(Qt::black);
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));

    }
    else
    {
        QCalendarWidget::paintCell(painter, rect, date);
    }


}

void QCustomCalendarWidget::initTopWidget()
{
    QWidget* topWidget = new QWidget(this);
    topWidget->setObjectName("CalendarTopWidget");
    topWidget->setFixedHeight(40);
    topWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

    QHBoxLayout* hboxLayout = new QHBoxLayout;
    hboxLayout->setContentsMargins(12, 0, 12, 0);
    hboxLayout->setSpacing(4);

    m_leftYearBtn   = new QPushButton(this);
    m_leftMonthBtn  = new QPushButton(this);
    m_rightYearBtn  = new QPushButton(this);
    m_rightMonthBtn = new QPushButton(this);
    m_dataLabel     = new QLabel(this);

    m_leftYearBtn->setObjectName("CalendarLeftYearBtn");
    m_leftMonthBtn->setObjectName("CalendarLeftMonthBtn");
    m_rightYearBtn->setObjectName("CalendarRightYearBtn");
    m_rightMonthBtn->setObjectName("CalendarRightMonthBtn");
    m_dataLabel->setObjectName("CalendarDataLabel");

    m_leftYearBtn->setFixedSize(16, 16);
    m_leftMonthBtn->setFixedSize(16, 16);
    m_rightYearBtn->setFixedSize(16, 16);
    m_rightMonthBtn->setFixedSize(16, 16);

    hboxLayout->addWidget(m_leftYearBtn);
    hboxLayout->addWidget(m_leftMonthBtn);
    hboxLayout->addStretch();
    hboxLayout->addWidget(m_dataLabel);
    hboxLayout->addStretch();
    hboxLayout->addWidget(m_rightMonthBtn);
    hboxLayout->addWidget(m_rightYearBtn);
    topWidget->setLayout(hboxLayout);

    //这里见下图1
    QVBoxLayout *vBodyLayout = qobject_cast<QVBoxLayout *>(layout());
    vBodyLayout->insertWidget(0, topWidget);

    connect(m_leftYearBtn,   SIGNAL(clicked()),  this, SLOT(onbotttonClicked()));
    connect(m_leftMonthBtn,  SIGNAL(clicked()),  this, SLOT(onbotttonClicked()));
    connect(m_rightYearBtn,  SIGNAL(clicked()),  this, SLOT(onbotttonClicked()));
    connect(m_rightMonthBtn, SIGNAL(clicked()),  this, SLOT(onbotttonClicked()));

    setDataLabelTimeText(selectedDate().year(), selectedDate().month());
}

void QCustomCalendarWidget::setDataLabelTimeText(int year, int month)
{
    m_dataLabel->setText(QStringLiteral("%1年%2月").arg(year).arg(month));
}

void QCustomCalendarWidget::onbotttonClicked()
{
    QPushButton *senderBtn = qobject_cast<QPushButton *>(sender());
    if (senderBtn == m_leftYearBtn)
    {
        showPreviousYear();
        setRangeDate(QDate());
    }
    else if (senderBtn == m_leftMonthBtn)
    {
        showPreviousMonth();
        setRangeDate(QDate());
    }
    else if (senderBtn == m_rightYearBtn)
    {
        showNextYear();
        setRangeDate(QDate());

    }
    else if (senderBtn == m_rightMonthBtn)
    {
        showNextMonth();
        setRangeDate(QDate());

    }
}




//void QCustomCalendarWidget::initBottomWidget()
//{
//    QWidget* bottomWidget = new QWidget(this);
//    bottomWidget->setObjectName("CalendarBottomWidget");
//    bottomWidget->setFixedHeight(40);
//    bottomWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

//    QHBoxLayout* hboxLayout = new QHBoxLayout;
//    hboxLayout->setContentsMargins(12, 0, 12, 0);
//    hboxLayout->setSpacing(6);

//    m_ensureBtn = new QPushButton(this);
//    m_ensureBtn->setObjectName("CalendarEnsureBtn");
//    m_ensureBtn->setFixedSize(40, 22);
//    m_ensureBtn->setText(QStringLiteral("确定"));

//    m_toDayBtn = new QPushButton(this);
//    m_toDayBtn->setObjectName("CalendarTodayBtn");
//    m_toDayBtn->setFixedSize(40, 22);
//    m_toDayBtn->setText(QStringLiteral("现在"));

//    hboxLayout->addStretch();
//    hboxLayout->addWidget(m_toDayBtn);
//    hboxLayout->addWidget(m_ensureBtn);
//    bottomWidget->setLayout(hboxLayout);

//    //这里见下图1
//    QVBoxLayout *vBodyLayout = qobject_cast<QVBoxLayout *>(layout());
//    vBodyLayout->addWidget(bottomWidget);

//    connect(m_ensureBtn, &QPushButton::clicked, [this](){
//        emit signalSetCalendarTime(selectedDate());
//        emit activated(selectedDate());
//    });

//    connect(m_toDayBtn, &QPushButton::clicked, [this](){
//        showToday();
//    });
//}
